package com.yjh.server.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjh.common.utils.FileUploadUtil;
import com.yjh.pojo.entity.CourtCategory;
import com.yjh.pojo.entity.CourtInfo;
import com.yjh.pojo.vo.R;
import com.yjh.server.service.CourtCategoryService;
import com.yjh.server.service.CourtInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CourtCategoryService courtCategoryService;


    @Autowired
    private CourtInfoService courtInfoService;

    @Autowired
    private FileUploadUtil fileUploadUtil;

    /**
     * 获取全部分类
     * @return
     */
    @GetMapping("/categoryList")
    public R<List> getAll(){

        List<CourtCategory> list = courtCategoryService.list();

        return R.success(list);
    }
    /**
     * 分页获取全部分类
     * @return
     */
    @GetMapping("/page")
    public R<Page> getByPage(@RequestParam(value="current",defaultValue="1") Integer current,@RequestParam(value="pageSize",defaultValue="3") Integer pageSize){

        Page<CourtCategory> page=new Page<>(current,pageSize);
        courtCategoryService.page(page);

        return R.success(page);
    }

    /**
     * 新增分类
     * @return
     * @throws IOException
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody CourtCategory courtCategory){

        courtCategoryService.save(courtCategory);

        return R.success("新增成功");
    }

    @DeleteMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public R<String> del(@RequestBody List<Integer> ids){
        try {
            //若有与该分类相关的球场信息表，则不能删除
            for(Integer id:ids){
                LambdaQueryWrapper<CourtInfo> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(CourtInfo::getCategoryId,id);
                int count = courtInfoService.count(wrapper);
                if(count>=1){
                    // 可以返回具体是哪个分类关联了球场
                    CourtCategory category = courtCategoryService.getById(id);
                    String categoryName = category != null ? category.getCategoryName() : "未知分类";
                    return R.error("分类【" + categoryName + "】还关联着球场，不可删除");
                }
            }
            courtCategoryService.removeByIds(ids);
            return R.success("删除成功");
        }catch (Exception e){
            throw new RuntimeException("删除失败"); // 触发事务回滚
        }
    }

    @PutMapping("/update")
    public R<String> update(@RequestBody CourtCategory courtCategory){
        courtCategoryService.updateById(courtCategory);
        return R.success("更新成功");
    }
}
